package com.neu.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableRowSorter;

import com.neu.dao.impl.ModelDao;
import com.neu.dao.impl.ResultDao;
import com.neu.pojo.Model;
import com.neu.pojo.Patient;
import com.neu.pojo.Result;
import com.neu.pojo.Worker;
import com.neu.service.impl.ModelService;
import com.neu.table.ResultTable;

import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.JTable;
import javax.swing.JButton;
import javax.swing.JComboBox;

public class EvaluateUI extends JFrame {

	private JPanel contentPane;
	private JTable table;
	private JComboBox comboBox = new JComboBox();
	private ResultTable resultTable;
	
	private ModelService modelService = ModelService.getInstance();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					EvaluateUI frame = new EvaluateUI(new Worker(),new Patient());
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public EvaluateUI(Worker w,Patient p) {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 690, 464);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		this.resultTable = new ResultTable();
		table = new JTable(this.resultTable);
		table.setBounds(35, 123, 610, 223);
		contentPane.add(table);
		
		JButton btnNewButton = new JButton("开始评估");
		btnNewButton.setBounds(35, 80, 97, 23);
		contentPane.add(btnNewButton);
		btnNewButton.addActionListener((e)->{
			String s=(String) comboBox.getSelectedItem();
			switch (s) {
				case "请选择模板":
					JOptionPane.showMessageDialog(null, "请选择一个模板");//提示
					break;
				default:
					Model m = modelService.getInstance().search(s);
					if(m!=null) {
						//传入 w p m做答题界面 w p m
						new AnswerUI(w, p, m).setVisible(true);//跳转新窗口
						setVisible(false);
					}else {
						JOptionPane.showMessageDialog(null, "这条应该不可能出现");//提示
					}
			}
			
		});
		
		comboBox.setBounds(484, 68, 135, 35);
		contentPane.add(comboBox);
		HashSet<String> names = new HashSet<>();
		for(Model m:ModelDao.getInstance().list()) {
			names.add(m.getName());
		}
		comboBox.addItem("请选择模板");
		for(String s:names) {
			comboBox.addItem(s);
		}
		comboBox.addActionListener((e)->{//根据用户选择的模板生成试题，
			TableRowSorter sorter = new TableRowSorter(resultTable);
			table.setRowSorter(sorter);
			String s=(String) comboBox.getSelectedItem();
			
			switch (s) {
		    	case "所有":
		    		break;
			    default:
			    	List<RowFilter<Object, Object>> filter=new ArrayList<>(2);
			    	filter.add(RowFilter.regexFilter(s));
			    	sorter.setRowFilter(RowFilter.andFilter(filter));
			    	break;
			}
		});
		
		JButton btnNewButton_1 = new JButton("返回");
		btnNewButton_1.setBounds(522, 378, 97, 23);
		contentPane.add(btnNewButton_1);
		btnNewButton_1.addActionListener((e)->{
			WorkerUI workerUI = new WorkerUI(w);//跳转新窗口
			workerUI.setVisible(true);
			setVisible(false);
		});
		
		//加滚动条
		JScrollPane jp = new JScrollPane(table);
		jp.setBounds(35, 123, 610, 223);
		contentPane.add(jp);
		
		//窗口居中
		setLocationRelativeTo(null);
	}
}
